package com.game.springbootstart.controller.user;


import com.game.springbootstart.VO.CommentLikesVO;
import com.game.springbootstart.VO.CommentsLikeVO;
import com.game.springbootstart.VO.CommentsVO;
import com.game.springbootstart.config.Result;
import com.game.springbootstart.entity.Comments;
import com.game.springbootstart.service.ICommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-03
 */
@RestController
@RequestMapping("/comments")
public class CommentsController {
    @Autowired
    private ICommentsService commentService;

    @PostMapping("/add")
    public Result addComment(@RequestBody Comments comments) {
        LocalDateTime NOW = LocalDateTime.now();
        comments.setCreatedAt(NOW);
        commentService.save(comments);
        Comments comments1 = commentService.getById(comments);
        return Result.success(comments1);
    }

    @PostMapping("/list")
    public Result getCommentsByGameId(@RequestBody CommentsVO commentsVO) {
        return Result.success(commentService.getCommentsByGameId(commentsVO));
    }

    @PostMapping("/toggleLike")
    public Result toggleLike(@RequestBody CommentsLikeVO commentsLikeVO) {
        return Result.success(commentService.updateBycommentId(commentsLikeVO.getCommentId(), commentsLikeVO.getLike(),commentsLikeVO.getUserId()));
    }

    @DeleteMapping("/delete")
    public Result deleteComment(@RequestParam Long commentId) {
        return Result.success(commentService.removeById(commentId));
    }

    @GetMapping("/getComments")
    public Result getComments(@RequestParam Long userId) {
        return Result.success(commentService.getCommentsByUserId(userId));
    }
}
